<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/28 0028
 * Time: 上午 11:14
 */

namespace Home\Model;
use Think\Model;

class BPCpxlModel extends Model{
    protected $filter;  //产品系列下的单品的筛选数据
    public function __construct(){
        parent::__construct();
        include_once ( "./Org/sphinx/sphinxapi.php" );

    }

    public function sphinx($name){
        $cl = new \SphinxClient();

        $cl->SetServer ( '127.0.0.1', 9312);
        $cl->SetConnectTimeout ( 3 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_BOOLEAN);//搜索模式
        $cl->SetLimits(0,10000);  //过滤
        $cl->SetSortMode (SPH_SORT_RELEVANCE); //排序
        $res = $cl->Query ($name,"cpxl" );
        //dump($res);exit;
        $cpxlnos = array_column($res['matches'],'id');
        return $cpxlnos;

    }
    //sphinx过滤
    public function sphinxFilter($name){
        $flno = I('get.flno');
        $gysno = I('get.gysno');
        $cl = new \SphinxClient ();
        $cl->SetServer ( '127.0.0.1', 9312);
        $cl->SetConnectTimeout ( 3 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_BOOLEAN);//搜索模式
        $cl->SetLimits(0,10000);  //过滤
        if($flno){
            $cl->SetFilter('flno',$flno,false) ; //以 分类ID 为条件 进行过滤
        }
        if($gysno){
            $cl->SetFilter('gysno',$gysno,false) ; //以 供应商ID 为条件 进行过滤
        }
        $cl->SetSortMode (SPH_SORT_RELEVANCE); //排序
        $res = $cl->Query ($name,"cpxl" );
        //dump($res);exit;
        $cpxlnos = array_column($res['matches'],'id');
        return $cpxlnos;

    }

    //获取筛选数据
    public function get_cpxllist_filter($cpxlnos){
        //$a = microtime(true);
        $filter = [];
        //取出供应商筛选数据
        $filter['gys'] = $this->alias('a')->field('a.gysno,b.qc')
            ->join('left join b_p_gysjbqk b on a.gysno=b.gysno')
            ->where(array('a.cpxlno' => array('in', $cpxlnos)))->group('a.gysno')->select();
        //取出分类筛选数据

        $filter['wzfl'] = $this->alias('a')->field('a.flno,b.flmc')
            ->join('left join b_c_wzfl b on a.flno=b.flno')
            ->where(array('a.cpxlno' => array('in', $cpxlnos)))->group('a.flno')->select();

        //dump(microtime(true)-$a); exit;
        return $filter;
    }




    /** 查询数据
     * @param $cpxlnos 系列ID集
     * @param int $length
     * @return array
     */
    public function search($cpxlnos,$length=50){
        //$a = microtime(true);
        $tiaoshu = I('post.tiaoshu');
        $flno = I('post.flno');
        $gysno = I('post.gysno');
        if($tiaoshu){
            $offset = $tiaoshu;        //计算偏移量
            $tiaoshu = $tiaoshu+$length; //计算已取条数
        }else{
            $offset = 0;
            $tiaoshu = $length;
        }
        if($flno){
            $where['a.flno'] = array('in',$flno);
        }
        if($gysno){
            $where['a.gysno'] = array('in',$gysno);
        }
        $where['cpxlno'] = array('in',$cpxlnos);
        $info = $this->alias('a')->field('a.cpxlno,a.xpxlmc,a.xh,b.flno,b.flmc,c.gysno,c.qc')
            ->join('left join b_c_wzfl b on a.flno=b.flno')
            ->join('left join b_p_gysjbqk c on a.gysno=c.gysno')
            ->where($where)->limit($offset,$length)->select();
        //dump(microtime(true)-$a); exit;
        return array(
            'info' => $info,
            'tiaoshu' => $tiaoshu
        );
    }

    //获取系列介绍单品-----3个
    public function getwxbminfo($cpxlno){
        $cpxlwzbmmodel = D('b_p_cpxlwzbm');
        $count = $cpxlwzbmmodel->where("cpxlno={$cpxlno}")->count();

        $sql = "select b.wzno,b.wzmc,b.xhgg,c.flmc,d.qc from b_p_cpxlwzbm a
                left join b_c_wzbm b on a.wzno=b.wzno
                left join b_c_wzfl c on b.wzfl=c.flno
                left join b_p_gysjbqk d on b.gysno=d.gysno
                where a.cpxlno={$cpxlno} limit 3";
        $info = D()->query($sql);

        return array(
            'info' => $info,
            'count' => $count
        );
    }

    public function getcpxlwzbmfilter($cpxlno){
        $filter = [];
        //取出属性筛选信息
        $filter['sx'] = $this->getcpxlwzbm_sx_filter($cpxlno);

        //取出型号和供应商筛选信息
        $filter['xh'] = $this->getcpxlwzbm_xh_filter($cpxlno);

        return $filter;
    }

    /**取出产品系列下的单品的型号信息
     * @param $cpxlno
     * @return array
     */
    public function getcpxlwzbm_xh_filter($cpxlno){
        $redis = linkredis();
        $filter = unserialize($redis->get($cpxlno.'_xl_wzbmfilter'));
        $sql = "select distinct xh from b_c_wzbm a
                join b_p_cpxlwzbm b on a.wzno=b.wzno
                where b.cpxlno={$cpxlno}";
        $xh_info = D()->query($sql);
        $xh_info = array_column($xh_info,'xh');
        if(count($xh_info) >= 2){
            $filter['xh'] = $xh_info;
        }else{
            $filter['xh'] = 'false';
        }
        //dump($filter);exit;
        $redis->set($cpxlno.'_xl_wzbmfilter',serialize($filter));
        $redis->expire($cpxlno.'_xl_wzbmfilter',3600*48);

        return $xh_info;
    }

    /**取出产品系列下的单品的属性信息
     * @param $cpxlno
     * @return array
     */
    public function getcpxlwzbm_sx_filter($cpxlno){
        $filter = [];
        $sql = "select row.sxno,row.sxmc,row.sxdw,GROUP_CONCAT(row.sxz) sxz  from
                (select distinct b.zdysxz sxz ,b.sxno,c.sxmc,c.sxdw from b_p_cpxlwzbm a
                join b_c_zdysx b on a.wzno=b.wzno
                join b_c_sxgz c on b.sxno=c.sxno
                where a.cpxlno={$cpxlno}) row
                group by row.sxno";
        $sx_info = D()->query($sql);

        foreach($sx_info as $k=>&$v){
            $v['sxz'] = array_unique(explode(',',$v['sxz']));
            if(count($v['sxz']) < 2 ){    //判断去重之后的属性值如果少于2个,就不做筛选条件
                unset($sx_info[$k]);
            }
        }
        //dump($sx_info);exit;
        $filter['sx'] = $sx_info;
        $redis = linkredis();
        $redis->set($cpxlno.'_xl_wzbmfilter',serialize($filter));
        $redis->expire($cpxlno.'_xl_wzbmfilter',3600*48);
        return $sx_info;
    }

    /**
     * 一个系列下的单品根据条件筛选函数
     * @param $cpxlno
     * @return array
     */
    public function get_tj_cpxlwzbm($cpxlno){
        $xh = I('get.xh');
        $sx = I('get.sx');
        $b_c_zdysx = D('b_c_zdysx');
        if ($xh && $sx) {  //如果型号和属性都存在
            //先取出型号和供应商筛选的数据,如果为空,直接返回空数组
            $xhwznos = $this->xh($xh,$cpxlno);
            if(!$xhwznos){
                return array();
            }else{
                //取出属性筛选的数据,如果有值,就和型号取出的数据求并集,如果没值,返回型号取出的数据
                $sx_wznos = $b_c_zdysx->filter_sx($sx,$cpxlno,'cpxl');
                if($sx_wznos){
                    $wznos = array_intersect($xhwznos,$sx_wznos);
                }else{
                    $wznos = $xhwznos;
                }
            }
            return $wznos;

        } elseif ($xh) {  //只有型号
            $wznos = $this->xh($xh,$cpxlno);
            //dump($wznos);exit;
            return $wznos;

        } elseif ($sx) {  //只有属性
            $wznos = $b_c_zdysx->filter_sx($sx,$cpxlno,'cpxl');
            return $wznos;
        }
    }


    public function xh($xh,$cpxlno){
        //拼接查询条件
        $str = "xh in ('".implode("','",$xh)."')";
        //echo $str;exit;
        $sql = "select b.wzno from b_c_wzbm a left join b_p_cpxlwzbm b on a.wzno=b.wzno
                where b.cpxlno={$cpxlno} and {$str}";
        //echo $sql;exit;
        $info = D()->query($sql);
        $info = array_column($info,'wzno');
        return $info;
    }


    //根据wzno查出系列ID和名称
    public function getxlinfo($wzno){
        $sql = "select b.cpxlno,b.xpxlmc from b_p_cpxlwzbm a join b_p_cpxl b on a.cpxlno=b.cpxlno
                where a.wzno={$wzno}";
        $info = D()->query($sql);
        return $info;
    }
}
